package nl.borchef.map_2d_game;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class splash_view extends SurfaceView implements SurfaceHolder.Callback {
	// start game_view class
	private Context mo_context;
	private splash_activity mo_activity;
	private int mi_surface_width = 0;
	private int mi_surface_height = 0;
    private Paint mo_paint_red;
    private Paint mo_paint_black;
    private SurfaceHolder mo_surface_holder = null;
	private splash_thread mo_thread = null;
    private GradientDrawable mDrawable;
	// game_view constructor
	public splash_view(Context context, splash_activity activity) {
		super(context);
		mo_activity = activity;
		mo_context = context;
		mo_paint_red = new Paint();
		mo_paint_red.setAntiAlias(true);
		mo_paint_red.setARGB(255, 255, 0, 0);
		mo_paint_red.setStrokeWidth(0);
		mo_paint_red.setTextSize(40);
		mo_paint_black = new Paint();
		mo_paint_black.setAntiAlias(true);
		mo_paint_black.setARGB(255, 0, 0, 0);
		mo_paint_black.setStrokeWidth(0);
		mo_paint_black.setTextSize(40);
		//Log.d(this.getClass().getName(), "class created");
		// register our interest in hearing about changes to our surface
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		mo_surface_holder = holder;
		this.setWillNotDraw(false);
	}

	public void onDraw(Canvas canvas){
        //Log.d(this.getClass().getName(), "ondraw" );
           	if( mi_surface_width <= 0 || mi_surface_height <= 0 ) return;
		int width = 500;
    	int height = 300;

    	Rect r = new Rect( (mi_surface_width / 2 ) - (width / 2),
    					   (mi_surface_height / 2) - (height / 2),
    					   (mi_surface_width / 2 ) + (width / 2),
    					    (mi_surface_height / 2) + (height / 2));
    	canvas.drawColor(Color.BLACK);
    	//canvas.drawRect(r, mo_paint_red);

        mDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] { 0xFFFF0000, 0xFFDF0000, 0xFFCF0000, 0xFFDF0000,
                    0xFFFF0000 });
		mDrawable.setShape(GradientDrawable.RECTANGLE);
		mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));
        
		canvas.save();
        canvas.translate(10, 10);
        mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		mDrawable.setCornerRadii(new float[] { 16, 16, 16, 16,
        		16, 16, 16, 16 });
        mDrawable.setBounds(r);
        mDrawable.draw(canvas);
        
        canvas.drawText("TankAttack 1", r.left + 10, r.top + 50, mo_paint_black);
        canvas.restore();
	
	}

	@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        //Log.d(this.getClass().getName(), "surface changed");
		mi_surface_width = width;
		mi_surface_height = height;
		this.invalidate();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
        // start the thread 
        //Log.d(this.getClass().getName(), "surface created");
		mo_thread = new splash_thread(holder, mo_context, new Handler() {
			@Override
			public void handleMessage(Message m) {
		        //Log.d(this.getClass().getName(), "handled message" + m.getData().getString("text"));
				if( mo_thread != null ){
			        mo_thread.surface_ready(false);
			        mo_thread.save_stop();
			        boolean retry = true;
			        while (retry) {
			            try {
			            	mo_thread.join();
			                retry = false;
			            } catch (InterruptedException e) {
			            }
			        }
			        mo_thread = null;
			        Intent intent = new Intent(mo_activity,game_activity.class);
			        mo_activity.startActivity(intent);
			        mo_activity.finish();
			        mo_activity = null;
				}
			}
		});
		mo_thread.start();
		mo_thread.surface_ready(true);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
        //Log.d(this.getClass().getName(), "surface destroyed");
		if( mo_thread != null ){
	        mo_thread.surface_ready(false);
	        mo_thread.save_stop();
	        boolean retry = true;
	        while (retry) {
	            try {
	            	mo_thread.join();
	                retry = false;
	            } catch (InterruptedException e) {
	            }
	        }
	        mo_thread = null;
		}
	}

} 
